package UI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class LoginJFrame extends JFrame implements MouseListener {

    static ArrayList<User> list = new ArrayList<>();

    static {
        list.add(new User("ZhangSan", "123"));
        list.add(new User("LiSi", "123"));
    }

    //添加登录按钮
    JButton login = new JButton();
    //添加注册按钮
    JButton register = new JButton();

    String codeStr;

    JTextField username = new JTextField();
    JTextField passwordK = new JTextField();
    JTextField verifyK = new JTextField();

    JLabel rightCode = new JLabel();


    public LoginJFrame() {


        InitJFrame();

        InitImage();


        this.setVisible(true);
    }

    private void InitImage() {

        //用户名
        JLabel user = new JLabel(new ImageIcon("src\\Image\\login\\用户名.png"));
        user.setBounds(116, 135, 47, 17);
        this.getContentPane().add(user);

        //输入框

        username.setBounds(195, 134, 200, 30);
        this.getContentPane().add(username);

        //密码
        JLabel password = new JLabel(new ImageIcon("src\\Image\\login\\密码.png"));
        password.setBounds(130, 195, 32, 16);
        this.getContentPane().add(password);
        //输入框

        passwordK.setBounds(195, 195, 200, 30);
        this.getContentPane().add(passwordK);

        //验证码
        JLabel verify = new JLabel(new ImageIcon("src\\Image\\login\\验证码.png"));
        verify.setBounds(133, 256, 50, 30);
        this.getContentPane().add(verify);
        //输入框

        verifyK.setBounds(195, 256, 100, 30);
        this.getContentPane().add(verifyK);

        //生成验证码
        codeStr = CodeUtil.getCode();

        rightCode.setText(codeStr);

        rightCode.setBounds(300, 256, 50, 30);
        rightCode.addMouseListener(this);
        this.getContentPane().add(rightCode);


        //设置登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon("src\\Image\\login\\登录按钮.png"));
        //去除边框
        login.setBorderPainted(false);
        //去除按钮背景
        login.setContentAreaFilled(false);
        this.getContentPane().add(login);

        login.addMouseListener(this);
        //设置注册按钮

        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("src\\Image\\login\\注册按钮.png"));
        //去除边框
        register.setBorderPainted(false);
        //去除按钮背景
        register.setContentAreaFilled(false);
        this.getContentPane().add(register);

        register.addMouseListener(this);

        //初始化背景
        JLabel background = new JLabel(new ImageIcon("src\\Image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }

    private void InitJFrame() {
        this.addMouseListener(this);

        this.setSize(488, 430);

        this.setTitle("登录");

        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(3);

        this.setLayout(null);
    }

    private void ShowJDialog(String context) {
        JDialog jDialog = new JDialog();

        jDialog.setSize(200, 150);

        jDialog.setAlwaysOnTop(true);

        jDialog.setLocationRelativeTo(null);

        jDialog.setModal(true);

        JLabel warning = new JLabel(context);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        jDialog.setVisible(true);
    }

    private boolean VerifyJudge(String str) {
//        for (int i = 0; i < str.length(); i++) {
//            char a=str.charAt(i);
//            char b=codeStr.charAt(i);
//            if(a!=b){
//                return false;
//            }
//        }
//        return true;
        if (codeStr.equalsIgnoreCase(str)) {
            return true;
        } else {
            return false;
        }
    }

    private boolean UsernameJudge(String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(str)) {
                return true;
            }
        }
        return false;
    }

    private boolean PasswordJudge(String str) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPassword().equals(str)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        Object obj = e.getSource();
        if (obj == rightCode) {
            System.out.println("点击");
            codeStr = CodeUtil.getCode();
            rightCode.setText(codeStr);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == login) {
            login.setIcon(new ImageIcon("src\\Image\\login\\登录按下.png"));
            this.getContentPane().repaint();


        } else if (obj == register) {
            register.setIcon(new ImageIcon("src\\Image\\login\\注册按下.png"));
            this.getContentPane().repaint();
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == login) {
            login.setIcon(new ImageIcon("src\\Image\\login\\登录按钮.png"));
            this.getContentPane().repaint();
            if (verifyK.getText().isEmpty()) {
                ShowJDialog("验证码不能为空!");
            } else if (username.getText().isEmpty() || passwordK.getText().isEmpty()) {
                ShowJDialog("用户名或密码不能为空！");
            } else {
                if (VerifyJudge(verifyK.getText())) {
                    if (UsernameJudge(username.getText())) {
                        if (PasswordJudge(passwordK.getText())) {
                            this.setVisible(false);
                            new GameJFrame();
                        } else {
                            ShowJDialog("密码错误");
                        }
                    } else {
                        ShowJDialog("用户名不存在");
                    }

                } else {
                    ShowJDialog("验证码错误");
                    codeStr = CodeUtil.getCode();
                    rightCode.setText(codeStr);
                    this.getContentPane().repaint();
                }
            }
        } else if (obj == register) {
            register.setIcon(new ImageIcon("src\\Image\\login\\注册按钮.png"));
            this.getContentPane().repaint();
        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
